package com.amazon.dao;


import com.amazon.bo.KeyWords;
import com.amazon.dao.base.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class KeyWordsDao extends BaseDao<KeyWords> {
    //保存
    public boolean save(KeyWords k){
        try{
            add(k);
            return true;
        }catch (Exception ex){
            return false;
        }
    }
    //查询
    public KeyWords getKeyWordsByCh(String k){
        KeyWords kw=execute(session ->
                session.createNativeQuery("SELECT * FROM `key_words` WHERE `chkey`=:chkey",KeyWords.class)
                        .setParameter("chkey",k).uniqueResult());
        return kw;
    }
    //更新
    public Boolean updateKey(KeyWords k){
        try{
            update(k);
            return true;
        }catch (Exception ex){
            return false;
        }
    }
    //关键词列表
    public List<KeyWords> getKeyWordsList(final int page, final int pageSize) {
        List<KeyWords> results = execute(session -> (List<KeyWords>) session
                .createNativeQuery("SELECT * FROM key_words ", KeyWords.class)
                .setFirstResult((page - 1) * pageSize)
                .setMaxResults(pageSize)
                .getResultList());
        return results != null ? results : new ArrayList<KeyWords>();
    }
}
